package com.ihelp.rest.mis.platform.controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.ihelp.rest.base.po.SysCode;
import com.ihelp.rest.mis.platform.service.ModuleService;
import com.ihelp.rest.mis.platform.service.UserService;
import com.ihelp.rest.mis.pojo.SysUser;
import com.ihelp.rest.utils.HtmlUtil;
import com.ihelp.rest.utils.SessionUtils;

/**
 * 
*平台登陆用户   
* @version
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private ModuleService moduleService;
	
	@Autowired
	private UserService userService;

	/**
	 * 登陆验证
	 * 成功：跳转主页面
	 * 失败：登陆页面 
	 * @param request
	 * @return
	 */
	@RequestMapping("login.do")
	 public void login(HttpServletRequest request, HttpServletResponse response, SysUser user) {
		String json = this.userService.loginUser(request, user);
		HtmlUtil.writerJson(response, json);
	 }
	
	/**
	 * 主页数据加载
	 * @param request
	 * @return
	 */
	@RequestMapping("main.do")
	 public String main(HttpServletRequest request) {
		SysUser user = SessionUtils.getUser(request);
		//功能模块
		if(user.getType() == 0){
			//超级管理员
			request.setAttribute("ModuleList", this.moduleService.findModules());
		}
		else{
			//普通用户
			request.setAttribute("ModuleList", user.getRole().getModules());
		}
		return "main";
	 }
	
	/**
	 * 加载用户列表页面 
	 * @param request
	 * @return
	 */
	@RequestMapping("loadUserPage.do")
	public String loadUserPage(HttpServletRequest request){
		return "system/user/user";
	}
	
	/**
	 * 用户列表数据查询JSON
	 * @param response
	 * @param page
	 * @param rows
	 */
	@RequestMapping("findUserPage.do")
	public void findUserPage(HttpServletResponse response, int page, int rows){
		String json = this.userService.findUserPage(page, rows);
		HtmlUtil.writerJson(response, json);
	}
	
	/**
	 * 查询所有用户JSON 数据
	 * @param response
	 */
	@RequestMapping("findAllUser.do")
	public void findAllUser(HttpServletResponse response){
		String json = this.userService.findAllUser();
		HtmlUtil.writerJson(response, json);
	}
	
	/**
	 * 加载用户页面
	 * @param request
	 * @param uuid
	 * @return
	 */
	@RequestMapping("loadAddUserPage.do")
	public String loadAddUserPage(HttpServletRequest request, String uuid){
		request.setAttribute("User", this.userService.getUser(uuid));
		return "system/user/addUser";
	}
	
	/**
	 * 新增用户
	 * @param response
	 */
	@RequestMapping("addUser.do")
	public void addUser(HttpServletResponse response, SysUser user){
		String text = this.userService.addUser(user);
		HtmlUtil.writerText(response, text);
	}
	
	/**
	 * 删除用户
	 * @param response
	 */
	@RequestMapping("delUser.do")
	public void delUser(HttpServletResponse response, SysUser user){
		String result = this.userService.delUser(user);
		//HtmlUtil.writerText(response, text);
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	/**
	 * 用户密码初始化
	 * @param response
	 * @param user
	 */
	@RequestMapping("initUserPass.do")
	public void initUserPass(HttpServletResponse response, String uuid){
		String text = this.userService.updateUserPass(uuid);
		HtmlUtil.writerText(response, text);
	}
	
	/**
	 * 加载密码修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping("loadUpdateUserPass.do")
	public String loadUpdateUserPass(HttpServletRequest request){
		return "updatePassword";
	}
	
	/**
	 * 用户密码修改
	 * @param response
	 * @param uuid
	 */
	@RequestMapping("updateUserPass.do")
	public void updateUserPass(HttpServletRequest request, HttpServletResponse response, String currentPassword, String newPassword){
		String text = this.userService.updateUserPass(request, currentPassword, newPassword);
		HtmlUtil.writerText(response, text);
	} 
	
	/**
	 * 用户登陆注销
	 * @param response
	 */
	@RequestMapping("loginOut.do")
	public void loginOut(HttpServletRequest request, HttpServletResponse response){
		SessionUtils.removeUser(request);
		HtmlUtil.writerText(response, SysCode.SUCCESS);
	}
	
	/**
	 * 上传文件
	 * @param inputId input控件的id
	 * @param request
	 * @return
	 */
	@RequestMapping("doUpLoadFileDemo.do")
	public void doUpLoadFile(HttpServletRequest request, MultipartFile fileToUpload){
		if(!fileToUpload.isEmpty()){
			String name = fileToUpload.getOriginalFilename();
			FileOutputStream out;
			try {
				out = new FileOutputStream("c:\\" + name);
				out.write(fileToUpload.getBytes());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
